import React from "react";

import PageContent from '../../PageContent'

import { ajax ,showResultMessage} from '../../common';

import {UIDataTableView} from '../../uicomponents/UIDataComponentView';

import UIFormView from '../../uicomponents/UIFormView';
/** 
 * 产品数据模型管理
*/

export default class ProductModelPackageManageApp extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            modelPackage: null,
        }
    }

    render() {
        return (
            <PageContent {...this.props} pageInstance={this} >
                <ProductModelPackageListPanel />
            </PageContent>
        );
    }
}

/** 
 * 产品模型包列表
*/
class ProductModelPackageListPanel extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            formTitle: '',
            formConfigId: 'addNewProductModelPackageForm',//productModelPackageForm
        }
    }

    actionHandler = (actionEvent) => {

        const { packageListRef, packageFormRef,selectPackageIds } = this;

        return {
            refresh: () => {
                packageListRef.loadData();
            },
            addNew: () => {
                this.setState({
                    formTitle: '新建模型包',
                    formConfigId: 'addNewProductModelPackageForm',
                });

                setTimeout(() => {
                    packageFormRef.openModal();
                });
            },
            doSave: () => {
                packageFormRef.getFormData((formData) => {
                    const result = {};
                    ajax('PRODUCT/MODEL/productModelPackage/saveModelPackage', formData).then((data) => {
                        if (data) {
                            packageFormRef.closeModal();
                            packageListRef.loadData();
                            result.success = true;
                            result.message = 'message.saveSuccess';
                        }
                    }).catch((message) => {
                        result.success = false;
                        result.message = message;
                    }).finally(() => {
                        showResultMessage(result);
                    });
                });
            },
            doEdit: () => {
                this.setState({
                    formTitle: '编辑模型包',
                    formConfigId: 'productModelPackageForm',
                });

                setTimeout(() => {
                    packageFormRef.openModal(() => {
                        packageFormRef.setFormData(actionEvent.params);
                    });
                });
            },
            doDelete : () => {
                let selectedIds = null;

				if (actionEvent.params != null) {
					selectedIds = [actionEvent.params.id];
				}
				else {
					selectedIds = selectPackageIds;
				}

				ajax('PRODUCT/MODEL/productModelPackage/deleteModelPackages', { ids: selectedIds }, (data) => {
					packageListRef.loadData();
				});
            }
        };
    }

    render() {

        const modelPackageProps = {
            ref: (inst) => { this.packageListRef = inst },
            actionHandler: this.actionHandler,
            actionGroupId: 'productModelPackageListActionGroup',
            rowActionGroupId: 'productModelPackageRowActionGroup',
            tableConfigId: 'modelPackageList',
            dataUrl: 'PRODUCT/MODEL/productModelPackage/queryModelPackages',
            selectMode: 'multi',
            searchFormId: 'productModelPackageSearchForm',
            autoLoad: true,
            setToolbarState: (topToolBar, selectedIds) => {
                if (selectedIds.length > 0) {
                    topToolBar.enable('delete');
                    topToolBar.enable('edit');
                }
                else {
                    topToolBar.disable('delete');
                    topToolBar.disable('edit');
                }
            },
            onSelectChange:
                (selectRowKeys) => {
                    this.selectPackageIds = selectRowKeys;
                }
            ,
        };

        const modelPackageFormProps = {
            ref: (inst) => { this.packageFormRef = inst },
            formConfigId: this.state.formConfigId,
            actionGroupId: 'saveActionGroup',
            actionHandler: this.actionHandler,
            inPop: true,
            destroyOnClose: true,
            title: this.state.formTitle,
        };

        return (
            <UIDataTableView {...modelPackageProps}>
                <UIFormView {...modelPackageFormProps} />
            </UIDataTableView>
        );
    }
}